<?php
    /**
     *  Copyright (c) 2013, advert-m102 Team
     *  All rights reserved.
     *
     *  Redistribution and use in source and binary forms, with or without
     *  modification, are permitted provided that the following conditions are met:
     *   1. Redistributions of source code must retain the above copyright
     *      notice, this list of conditions and the following disclaimer.
     *   2. Redistributions in binary form must reproduce the above copyright
     *      notice, this list of conditions and the following disclaimer in the
     *      documentation and/or other materials provided with the distribution.
     *   3. All advertising materials mentioning features or use of this software
     *      must display the following acknowledgement:
     *      This product includes software developed by the advert-m102.
     *   4. Neither the name of the advert-m102 nor the
     *      names of its contributors may be used to endorse or promote products
     *     derived from this software without specific prior written permission.
     *
     *  THIS SOFTWARE IS PROVIDED BY advert-m102 ''AS IS'' AND ANY
     *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     *  DISCLAIMED. IN NO EVENT SHALL advert-m102 BE LIABLE FOR ANY
     *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     *
     */

    class Controller extends Controller_files
    {

        const   URL_GET_PARAM = "page"; // $_GET param to load pages
        const   DEFAULT_PAGE = "search"; // $_GET param to load pages

        static $_page_template;

        static $_page = array(
            'run' => "",
            'cmd' => ""
        );

        static $_page_css = array( //  Our Base Css files
            'bootstrap.min.css',
            // 'bootstrap.css',
            'bootstrap-theme.min.css',
            'bootstrap-responsive.css',
            'layout.css',
        );

        static $_page_javascript = array( // Base Javascript
            'jquery.min.js',
            'main.js',
            // 'jquery-ui.min.js',
            'bootstrap.min.js',
            'jquery.chosen.min.js',
            'off_snowstorm.js',
            'bootstrap.js',
            'bootstrap-alert.js',
            'bootstrap-button.js',
            'bootstrap-carousel.js',
            'bootstrap-collapse.js',
            'bootstrap-dropdown.js',
            'bootstrap-modal.js',
            'bootstrap-popover.js',
            'bootstrap-scrollspy.js',
            'bootstrap-tab.js',
            'bootstrap-tooltip.js',
            'bootstrap-transition.js',
            'bootstrap-typehead.js'
        );


        /**
         *
         *  Get input params from URL
         *  main
         *              ?page=<page_to_load>
         *           or ?<page_to_load>
         */
        static public function init()
        {
            self::$_page['run'] = Input::In_GET_Url(self::URL_GET_PARAM);


            if (!self::$_page['run']) {
                $query              = explode('&', htmlspecialchars($_SERVER['QUERY_STRING']));
                self::$_page['run'] = !empty($query[0]) ? $query[0] : self::DEFAULT_PAGE;
            }

            self::$_page['cmd']   = Input::In_GET_Url('cmd');
            self::$_page_template = new Template();
        }

        /**
         *     no HTML5 support
         *
         */
        public static function run_on_old_browser()
        {
            $tmp     = new Browser();
            $_return = NULL;

            if (!$tmp->HTML5()) {

                $_return = self::header_on_noHTML5();
                echo self::_error_light("incompatible Web Browser", ' Your browser is not <a href="http://en.wikipedia.org/wiki/HTML5">HTML5 Compatible</a>  and you may not see correctly this web site ! You must upgrade your browser');
                echo '<!-- ' . $tmp->getBrowser() . ' ver ' . $tmp->getVersion() . ' ' . $tmp->getPlatform() . $tmp->getUserAgent() . ' -->';

            }


            return $_return;
        }


        /**
         * @return mixed
         *
         *   return Main Page Code , include Template
         *
         */

        static public function create_page()
        {

            $_show_file = self::_Execute(self::$_page['run']);

            if (self::$_page['run'] && !$_show_file) {
                $_show_file = self::_error("Page Not found", ' οοπς ... <br/> Έγινε ένα λάθος , η σελίδα δε βρέθηκε !!!  <br/> <br/> <p><a onclick="window.history.back()"  class="btn btn-danger btn-sm " role="button">&laquo; επιστροφή </a></p>');
            }


            self::$_page_template->_autorun_template();
            self::$_page_template->set('page', $_show_file);
            self::$_page_template->set('copyright', COPYRIGHT);


            return self::$_page_template->output();
        }


        /**
         *
         *  Main Action
         *
         */
        static public function run()
        {

            self::init();

            /*
             *  Make permanent static link on Mysql
             */
            MyDB::init_db();

            $nss = new Html5();

            $_html = $nss->Header();

            $_html .= self::run_on_old_browser();

            foreach (self::$_page_javascript as $file)
                $_html .= Html::javascript($file);

            foreach (self::$_page_css as $file)
                $_html .= Html::css($file);

            $_html .= '<body>';

            Echoc::output($_html);

            $_html = self::create_page();
            $_html .= Html::javascript("google_analytics.js");
            $_html .= $nss->Footer();

            /*
             *   HTML output
             */
            Echoc::output($_html);
        }
    }